package com.bjsxt.userscenter.system.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.system.dao.IARegionDAO;
import com.bjsxt.userscenter.system.pojo.ARegion;
import com.bjsxt.userscenter.system.pojo.ARegionEnum;
import com.bjsxt.userscenter.system.service.ISystemService;

/**
 * 数据模块的实现类
 * 
 * @author wangshh
 * 
 */
@Service("systemService")
public class SystemServiceImplay implements ISystemService
{
	@Resource
	private IARegionDAO regionDAO;
	
	@Override
	public JSONObject saveOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		/* 处理树形结构 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", region.getParentId());
		ARegion regionRes = this.findOneRegionService(condMap);
		if(regionRes != null)
		{
			/* 更新父节点的叶子节点 */
			if(regionRes.getLeafStatus() == ARegionEnum.LEAFSTATUS_FALSE.getStatus())
			{
				regionRes.setLeafStatus(ARegionEnum.LEAFSTATUS_TRUE.getStatus());
				regionRes.setUpdateTime(new Date());
				this.updateOneRegionService(regionRes);
			}
		}
		
		int res = this.regionDAO.save(region);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功;结果:" + res);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.regionDAO.update(region);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功;结果:" + res);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject deleteOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.regionDAO.save(region);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功;结果:" + res);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON ; 
	}

	@Override
	public List<ARegion> findCondListRegionService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		List<ARegion> regionList = Collections.EMPTY_LIST;
		/* 设置分页相关信息 */
		if(pageInfoUtil != null)
		{
			/* 设置总记录数 */
			condMap.put("totalCountFlag", "true");
			regionList = this.regionDAO.findCondList(condMap);
			if(regionList.size() == 1)
			{
				ARegion users = regionList.get(0);
				pageInfoUtil.setTotalRecord(users.getId());
			}
			
			condMap.put("totalCountFlag", "false");
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord()) ; 
			condMap.put("pageSize", pageInfoUtil.getPageSize());
		}
		regionList = this.regionDAO.findCondList(condMap) ; 
		return regionList; 
	}

	@Override
	public ARegion findOneRegionService(Map<String, Object> condMap)
	{
		ARegion region = this.regionDAO.findOne(condMap);
		return region ; 
	}

	@Override
	public List<ARegion> findTreeRegionService(ARegion region)
	{
		List<ARegion> regionList = new ArrayList<ARegion>(); 
		if(region != null)
		{
			this.findTreeRegionServiceUtil(regionList,region);
			Collections.reverse(regionList);
			
			//获得字符串表示
			StringBuffer ressb = new StringBuffer();
			for (Iterator iterator = regionList.iterator(); iterator.hasNext();)
			{
				ARegion regTemp = (ARegion) iterator.next();
				ressb.append(regTemp.getName() + ConstatFinalUtil.SPLIT_STR);
			}
			
			if(!"".equalsIgnoreCase(ressb.toString()))
			{
				ressb.delete(ressb.lastIndexOf(ConstatFinalUtil.SPLIT_STR), ressb.length());
			}
			region.setTreeName(ressb.toString());
		}
		return regionList ; 
	}
	
	/**
	 * 计算树形结构的工具方法,一级节点-->二级节点-->三级节点
	 * @param ressb
	 * @param regionid
	 */
	private void findTreeRegionServiceUtil(List<ARegion> regList, ARegion region)
	{
		if(region == null)
		{
			return ;
		}
		regList.add(region);
		if(region.getParentId() != 0)
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", region.getParentId() + "");
			ARegion regionRes = this.findOneRegionService(condMap);
			//ARegion regionRes = ConstatFinalUtil.regionMap.get(region.getParentid());
			findTreeRegionServiceUtil(regList, regionRes);
		}
	}
}
